package com.taotao.portal.controller;

import java.io.UnsupportedEncodingException;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.taotao.portal.service.SearchService;
import com.taotao.rest.webservice.SearchResult;

@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;

	@RequestMapping(value = "/search", produces="application/json; charset=utf-8")
	@ResponseBody
	public ModelAndView getItem(@RequestParam("q") String query, @RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "30") int rows) throws UnsupportedEncodingException {
		query = new String(query.getBytes("iso-8859-1"), "utf-8");
		SearchResult result = searchService.getItemList(query, page, rows);
		ModelAndView mav = new ModelAndView("search");
		mav.addObject("query", query);
		mav.addObject("page", page);
		mav.addObject("itemList", result.getItemList());
		mav.addObject("totalPages", result.getPageCount());
		return mav;
	}
}
